<?php
class Deal_category extends MY_Controller {

	var $currentPage = "deal_category";
	var $caption = "Deal Category";	
	
	public function index() {
		
		$data['categorydeallist'] = $this->mDealCategory->getAllEntries();
		$this->loadAdminPage($data);
		//$this->loadClientPage($data = "");
	}

	public function create($data='') {
		$this->action = 'add';
		
		$data =  array();
		$this->loadAdminPage($data);
	}
	
	public function add() {
		$dealcategory = $this->mDealCategory;
		
		$dealcategory->name = trim($this->input->post('name',TRUE));
		$dealcategory->code = trim($this->input->post('code',TRUE));
		$dealcategory->description = trim($this->input->post('description',TRUE));
		
		$this->message = INSERT_ERROR;
		
		if(strlen($dealcategory->name) != 0 && strlen($dealcategory->code) != 0 ) {
			if(count($this->mDealCategory->find_by_code($dealcategory->code))==0) {
				$this->message = INSERT_SUCCESS;
				$dealcategory->insert();
			} else {
				$this->message = HAS_DUPLICATE . " name.";
				$data['dealcategory'] = $dealcategory;
				$this->create($data);
				return;
			}
		} else {
			$data['dealcategory'] = $dealcategory;
			$this->create($data);
			return;
		}
		
		$this->index();
	}
	
	public function edit($id) {
		$dealcategory = $this->mDealCategory->getEntryById($id);
		
		if(count($dealcategory) == 0) {
			show_error("Deal Category ", 500, "Invalid data");
		}
	
		$data['dealcategory'] = $dealcategory[0];
		
		$this->action = 'edit';
		$this->id = $dealcategory[0]->id;
		$this->loadAdminPage($data);		
	}
	
	public function update() {
		$dealcategory = $this->mDealCategory;
		
		$dealcategory->id = $this->input->post('id',TRUE);
		$dealcategory->name = trim($this->input->post('name',TRUE));
		$dealcategory->code = trim($this->input->post('code',TRUE));
		$dealcategory->description = trim($this->input->post('description',TRUE));
		$this->message = UPDATE_ERROR;
		
		if(strlen($dealcategory->id) != 0  && strlen($dealcategory->name) != 0 && strlen($dealcategory->code) != 0) {
			
			if(count($this->mDealCategory->find_by_code_of_others($dealcategory->id,$dealcategory->code))==0) {
				if($dealcategory->update())
					$this->message = UPDATE_SUCCESS;
			} else {
				$this->message = HAS_DUPLICATE . " code.";
			}
		}
		
		$this->edit($dealcategory->id);
	}
	
	public function delete($id) {
		$this->message = DELETE_ERROR;
		
		if($this->mDealCategory->delete($id))
			$this->message = DELETE_SUCCESS;
		
		$this->index();		
	}
}
?>